package com.qf.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.dao.MerchantClassificationMapper;
import com.qf.entitys.MerchantClassification;
import com.qf.service.MerchantClassificationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MerchantClassificationServiceImpl implements MerchantClassificationService {

    @Resource
    private MerchantClassificationMapper mapper;

    @Override
    public int deleteByPrimaryKey(Integer mcId) {
        return mapper.deleteByPrimaryKey(mcId);
    }

    @Override
    public int insert(MerchantClassification record) {
        return 0;
    }

    @Override
    public int insertSelective(MerchantClassification record) {
        return mapper.insertSelective(record);
    }

    @Override
    public MerchantClassification selectByPrimaryKey(Integer mcId) {
        return mapper.selectByPrimaryKey(mcId);
    }

    @Override
    public int updateByPrimaryKeySelective(MerchantClassification record) {
        return mapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(MerchantClassification record) {
        return 0;
    }

    @Override
    public PageInfo<MerchantClassification> selectAll(Integer pageindex) {
        //使用pagehelper设置查询第几页，一页显示多少条记录
        PageHelper.startPage(pageindex,3);
        List<MerchantClassification> merchantClassificationList = mapper.selectAll();
        PageInfo<MerchantClassification> pageInfo = new PageInfo<>(merchantClassificationList);
        return pageInfo;
    }

    /**
     * 批量删除，通过事务来提交，若出现异常则回滚删除操作
     * @param ids
     * @return
     */
    @Override
    public int deleteByIds(Integer[] ids) {
        for (Integer id : ids) {
            int i = deleteByPrimaryKey(id);
            if (i==0){
                //出现错误就向上抛异常，事务默认遇到运行时异常就会回滚操作
                throw new RuntimeException("删除失败");
            }
        }
        //如果没问题就返回1
        return 1;
    }

    @Override
    public List<MerchantClassification> selectAll() {
        return mapper.selectAll();
    }
}
